package core;

import graphics.ImageHandler;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.util.Collection;
import java.util.Vector;

public class MessageHandler {
	
	private static int messageWidth=800, messageHeight = 150, bottomMarginal=20, borderWidth=5, cornerRoundness=20;
	private static int messageShowTime = 700;
	private static int timeLeft;
	private static boolean isShowingMessage = true;
	static Dimension screenSize = Constants.screenSize;
	
	static private Vector<String> messageList = new Vector<String>();
	static private Vector<String> imageList = new Vector<String>();
	
	//static private Vector<String> objectiveList = new Vector<String>();
	//static private char[] objectiveMessage = "fdajaenlkad dafjklean daf eed".toCharArray();
	//static private Collection<Integer> objectiveSplitPositions = new Vector<Integer>();
	//static private Vector<String> imageList = new Vector<String>();
	
	static char[] currentMessage = "Greetings Mortal!".toCharArray();
	static private Collection<Integer> splitPositions = new Vector<Integer>();
	static private int currentTypePos, lastSplitPos;
	static int imageId;
	static Font font = new Font("Monospaced", Font.BOLD, 20);
	static FontMetrics fm;
	
	public static void paint(Graphics g){
		if(isShowingMessage){
			g.setColor(Color.black);
			g.fillRoundRect(screenSize.width/2-messageWidth/2, screenSize.height-messageHeight-bottomMarginal, messageWidth, messageHeight, cornerRoundness, cornerRoundness);
			g.setColor(Color.LIGHT_GRAY);
			g.fillRoundRect(screenSize.width/2-messageWidth/2+borderWidth, screenSize.height-messageHeight-bottomMarginal+borderWidth, messageWidth-2*borderWidth, messageHeight-2*borderWidth, cornerRoundness, cornerRoundness);
			ImageHandler.getInstance().drawImage(screenSize.width/2-messageWidth/2+20, screenSize.height-messageHeight-bottomMarginal+20, messageHeight-40, messageHeight-40, imageId, g);
			g.setColor(Color.black);
			g.setFont(font);

			int y = screenSize.height-messageHeight-bottomMarginal+15;
			fm=g.getFontMetrics();
			int yInc = fm.getHeight();
			
			int lastI = 0;
			for(int i : splitPositions){
				y = y + yInc;
				g.drawChars(currentMessage, lastI, i-lastI, screenSize.width/2-messageWidth/2+messageHeight, y);
				lastI = i;
			}
			y = y + yInc;
			g.drawChars(currentMessage, lastI, currentTypePos-lastI, screenSize.width/2-messageWidth/2+messageHeight, y);
		}
		
	}
	
	public static void showMessage(String message, String imageName){
		currentMessage = message.toCharArray();
		imageId = ImageHandler.getInstance().getIdOfImage(imageName);
		timeLeft = messageShowTime;
		isShowingMessage = true;
		currentTypePos=0;
		lastSplitPos=0;
		splitPositions.clear();
		
	}
	
	public static void clearMessages(){
		messageList.clear();
		imageList.clear();
	}
	
	public static void addMessage(String message, String imageName){
		messageList.add(message);
		imageList.add(imageName);
	}
	
	public static void showNextMessage(){
		if(!messageList.isEmpty()){
			showMessage(messageList.get(0), imageList.get(0));
			splitPositions.clear();
			messageList.remove(0);
			imageList.remove(0);
		}
	}
	
	
	
	public static void tick(){
		timeLeft--;
		
		
		if(currentTypePos <= currentMessage.length-1 && timeLeft < messageShowTime-3){
			currentTypePos++;
			timeLeft = messageShowTime;
		}
		if(fm != null && fm.charsWidth(currentMessage, lastSplitPos, currentTypePos-lastSplitPos) > (messageWidth-messageHeight)-100
				&& currentMessage[currentTypePos-1] == ' '){  // currentTypePos-lastSplitPos-1
			lastSplitPos=currentTypePos;
			splitPositions.add(lastSplitPos);
		}
		
		if(timeLeft <= 0){
			isShowingMessage = false;
			showNextMessage();
		}
		
	}
	
	/**
	 * 
	 * @param f amount of frames to show all messages
	 */
	public static void setShowTime(int f){
		messageShowTime = f;
	}
	
	
	
}

